<?php

namespace App\Admin\Controllers;

use App\EloquentORM\Category;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Column;
use Encore\Admin\Layout\Content;
use Encore\Admin\Layout\Row;

use Encore\Admin\Tree;
use Encore\Admin\Widgets\Box;



class CategoryController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '文章分类';
	
	
	public function index(Content $content)
	{
		return $content
			->title($this->title)
			->description($this->description['index'] ?? trans('admin.list'))
			->row(function (Row $row) {
				$row->column(6, $this->treeView()->render());
				$row->column(6, function (Column $column) {
					$form = new \Encore\Admin\Widgets\Form();
					$form->action(admin_url('categories'));
					$form->select('parent_id', '父级分类')->options(Category::selectOptions());
					$form->text('name', '分类名称')->rules('required');
					$form->hidden('_token')->default(csrf_token());
					$column->append((new Box('新增', $form))->style('success'));
				});
			});
	}
	
	
	protected function treeView()
	{
		return Category::tree(function (Tree $tree) {
			$tree->disableCreate();
			$tree->branch(function ($branch) {
				$payload = "<strong>{$branch['name']}</strong>";
				return $payload;
			});
		});
	}
	
	/**
	 * Make a grid builder.
	 *
	 * @return void
	 */
    protected function grid()
    {
    
    }
	
	/**
	 * Make a show builder.
	 *
	 * @param mixed $id
	 * @return void
	 */
    protected function detail($id)
    {
        return ;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new Category);
	
	    $form->select('parent_id', '父级分类')->options(Category::selectOptions());
	    $form->text('name', '分类名称')->rules('required');
	    return $form;
    }
}
